//
//  EMIconUnderLineTextF.m
//  EducationManagementSystem
//
//  Created by 花花蔡 on 2017/1/7.
//  Copyright © 2017年 com.HuaHuaCai. All rights reserved.
//

#import "EMIconUnderLineTextF.h"
#import "EMSysButton.h"

#define TextF_UNDERLINE [UIColor cz_colorWithHex:0x90b4e9]


@interface EMIconUnderLineTextF()<UITextFieldDelegate> {
    
    
}

@property(nonatomic,strong) UILabel *placeholderLabel;


@end


@implementation EMIconUnderLineTextF {
    UIColor *_fontColor;
    CGRect _frame;
}

- (instancetype)initWithFrame:(CGRect)frame andImageName:(NSString *)imageName withPlaceholder:(NSString *)placeholder
{
    self = [super initWithFrame:frame];
    if (self) {
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:AAdaptionRect(-(26+62), 0, 62, 62)];
        imageView.image = [UIImage imageNamed:imageName];
        [self addSubview:imageView];
        
        UIView *underLineView = [[UIView alloc] initWithFrame:AAdaptionRect(0, CGRectGetMaxY(imageView.frame)/AAdaptionWidth() - 2, self.frame.size.width/AAdaptionWidth(), 2)];
        underLineView.backgroundColor = TextF_UNDERLINE;
        [self addSubview:underLineView];
        self.placeholder = placeholder;
        self.delegate = self;
        
    }
    return self;
}


-(instancetype)initWithFrame:(CGRect)frame withPlaceholder:(NSString *)placeholder WithFontColor:(UIColor *)fontColor WithAnimationType:(AnimationType)type {
    
    if ([super initWithFrame:frame]) {
        
        UIView *underLineView = [[UIView alloc] initWithFrame:AAdaptionRect(0, CGRectGetMaxY(self.bounds)/AAdaptionWidth()+4, self.frame.size.width/AAdaptionWidth(), 2)];
        underLineView.backgroundColor = [UIColor whiteColor];
        
        _placeholderLabel = [[UILabel alloc] initWithFrame:AAdaptionRect(0, 4, frame.size.width/2/AAdaptionWidth(), frame.size.height/AAdaptionWidth() - 8)];
        _placeholderLabel.text = placeholder;
        _placeholderLabel.font = [UIFont fontWithName:ManYu_FontStr size:15.f];
        _placeholderLabel.adjustsFontSizeToFitWidth = YES;
        _placeholderLabel.textColor = fontColor;
        _fontColor = fontColor;
        _frame = frame;
        self.type = type;
        
        
        [self addSubview:_placeholderLabel];
        [self addSubview:underLineView];
        self.delegate = self;


        
    }
    
    return self;
}


//- (instancetype)initWithCountDownBtnWithFrame:(CGRect)frame withPlaceholder:(NSString *)placeholder WithFontColor:(UIColor *)fontColor WithBtnClicked:(ButtomClickedWithSender)senderBlock{
//
//    if ([super initWithFrame:frame]) {
//        
//        UIView *underLineView = [[UIView alloc] initWithFrame:AAdaptionRect(0, CGRectGetMaxY(self.bounds)/AAdaptionWidth()+4, self.frame.size.width/AAdaptionWidth(), 2)];
//        underLineView.backgroundColor = [UIColor whiteColor];
//        
//        _placeholderLabel = [[UILabel alloc] initWithFrame:AAdaptionRect(0, 4, frame.size.width/2/AAdaptionWidth(), frame.size.height/AAdaptionWidth() - 8)];
//        _placeholderLabel.text = placeholder;
//        _placeholderLabel.adjustsFontSizeToFitWidth = YES;
//        _placeholderLabel.textColor = fontColor;
//        _fontColor = fontColor;
//        _frame = frame;
//        self.type = WithCountDownBtn;
//        
//        
//        CGFloat btn_X = CGRectGetMaxX(self.frame)/AAdaptionWidth() - 30/AAdaptionWidth();
//        EMSysButton *btn = [[EMSysButton alloc] initCountdownBtnWithFrame:AAdaptionRect(btn_X, 0, 230, frame.size.height/AAdaptionWidth()) WithTitleColor:[UIColor blackColor] WithBKColor:[UIColor whiteColor] WithClickedBlock:^(id sender) {
//            //NSLog(@"点了倒计时btn");
//            senderBlock(sender);
//        }];
//        btn.userInteractionEnabled = YES;
//        [self addSubview:btn];
//        
//        
//        [self addSubview:_placeholderLabel];
//        [self addSubview:underLineView];
//        self.delegate = self;
//        
//        
//        
//    }
//    
//    return self;
//
//}

#pragma mark - delegate

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    
    switch (self.type) {
        case top:
        {
            if ([self isFirstResponder]) {
                [UIView animateWithDuration:0.5 animations:^{
                    _placeholderLabel.frame = AAdaptionRect(0, -100, 120, 90);
                    _placeholderLabel.adjustsFontSizeToFitWidth = YES;
                    
                }];
            }
            
            
        }
            break;
        case bottom: {
            __weak typeof(self) weakSelf = self;

            if ([self isFirstResponder]) {
                [UIView animateWithDuration:0.5 animations:^{
                    
                    weakSelf.frame = AAdaptionRect(weakSelf.frame.origin.x/AAdaptionWidth(), (weakSelf.frame.origin.y + 30)/AAdaptionWidth(),weakSelf.frame.size.width/AAdaptionWidth(),weakSelf.frame.size.height/AAdaptionWidth());
                    _placeholderLabel.frame = AAdaptionRect(0, -100, 120, 90);
                    _placeholderLabel.adjustsFontSizeToFitWidth = YES;
                }];
            }
            
        }
            break;
        default:
            break;
    }
    

    return YES;
}        // return NO to disallow editing.
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    
}           // became first responder
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    
    
    switch (self.type) {
        case top:
        {
            if ([self.text isEqualToString:@""]) {
                //typeof(self) weakSelf = self;
                [UIView animateWithDuration:1 animations:^{
                    
                    _placeholderLabel.frame = AAdaptionRect(0, 4, _frame.size.width/2/AAdaptionWidth(), _frame.size.height/AAdaptionWidth() - 8);
                    _placeholderLabel.adjustsFontSizeToFitWidth = YES;
                    _placeholderLabel.textColor = _fontColor;
                }];
            }
        }
            break;
        case bottom:
            {
                if ([self.text isEqualToString:@""]) {
                    __weak typeof(self) weakSelf = self;
                    [UIView animateWithDuration:1 animations:^{
                        
                        weakSelf.frame = _frame;
                        _placeholderLabel.frame = AAdaptionRect(0, 4, _frame.size.width/2/AAdaptionWidth(), _frame.size.height/AAdaptionWidth() - 8);
                        _placeholderLabel.adjustsFontSizeToFitWidth = YES;
                        _placeholderLabel.textColor = _fontColor;
                    }];
                }
                
            }
            break;
        default:
            break;
    }
    return YES;
}          // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
- (void)textFieldDidEndEditing:(UITextField *)textField{
    
}             // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
- (void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason NS_AVAILABLE_IOS(10_0){
    
} // if implemented, called in place of textFieldDidEndEditing:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    return YES;
}   // return NO to not change text

- (BOOL)textFieldShouldClear:(UITextField *)textField{
    return YES;
}               // called when clear button pressed. return NO to ignore (no notifications)
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self resignFirstResponder];
    return YES;
}              // called when 'return' key pressed. return NO to ignore.



@end
